package com.lanxiao.cloud.modules.user.controller;

import com.lanxiao.cloud.common.core.dto.PageQuery;
import com.lanxiao.cloud.common.core.dto.ResultModel;
import com.lanxiao.cloud.common.core.enums.OperationType;
import com.lanxiao.cloud.common.web.invalid.Log;
import com.lanxiao.cloud.modules.user.application.UserCmdManager;
import com.lanxiao.cloud.modules.user.application.UserQueryManager;
import com.lanxiao.cloud.modules.user.application.dto.UserCmdUpdate;
import com.lanxiao.cloud.modules.user.application.dto.UserDeptPageDto;
import com.lanxiao.cloud.modules.user.application.dto.UserDeptQuery;
import com.lanxiao.cloud.modules.user.application.dto.UserDto;
import com.lanxiao.cloud.modules.user.application.dto.UserPageDto;
import com.lanxiao.cloud.modules.user.application.dto.UserPersonalCmdUpdate;
import com.lanxiao.cloud.modules.user.application.dto.UserQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：qiaoliang
 * @date ：2021-09-13
 */
@Api(tags = "用户服务")
@Validated
@RequestMapping("/users")
@RestController
public class UserController {

  private final UserCmdManager cmdManager;
  private final UserQueryManager queryManager;

  public UserController(UserCmdManager cmdManager,
      UserQueryManager queryManager) {
    this.cmdManager = cmdManager;
    this.queryManager = queryManager;
  }

  @ApiOperation(value = "分页查询用户信息")
  @GetMapping
  public ResultModel<UserPageDto> query(UserQuery query, PageQuery page){
    return queryManager.query(query, page);
  }

  @ApiOperation(value = "分页查询部门用户信息")
  @GetMapping("/dept")
  public ResultModel<UserDeptPageDto> query(UserDeptQuery query, PageQuery page){
    return queryManager.query(query, page);
  }

  @ApiOperation(value = "昵称查询用户信息")
  @GetMapping("/{name}")
  public List<UserDto> list(@PathVariable("name") String name) {
    return queryManager.list(name);
  }

  @ApiOperation(value = "查询登录用户信息")
  @GetMapping("/info")
  public UserDto info() {
    return queryManager.info();
  }

  @ApiOperation(value = "修改用户信息")
  @Log(operationType = OperationType.UPDATE)
  @PutMapping("/{id}")
  public ResponseEntity<?> update(@PathVariable Long id, @RequestBody UserCmdUpdate update) {
    cmdManager.update(id, update);
    return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).build();
  }

  @ApiOperation(value = "修改用户部门")
  @Log(operationType = OperationType.UPDATE)
  @PutMapping("/dept/{id}")
  public ResponseEntity<?> updateDept(@PathVariable Long id, @RequestBody Long deptId) {
    cmdManager.updateDept(id, deptId);
    return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).build();
  }

  @ApiOperation(value = "修改用户个人信息")
  @Log(operationType = OperationType.UPDATE)
  @PutMapping
  public ResponseEntity<?> update(@RequestBody UserPersonalCmdUpdate update) {
    cmdManager.update(update);
    return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).build();
  }

}
